package com.springboot.hotel.controller.sysuser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.springboot.hotel.model.SysUser;
import com.springboot.hotel.service.impl.SysUserServiceImpl;
import com.springboot.hotel.xencryption.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

//用户信息管理表
@RestController
public class CustInfoMgmtController {
    @Autowired
    SysUserServiceImpl service;

    //    跳转页面到用户信息管理
    @GetMapping("/sysUser/CustInfoMgmt")
    public ModelAndView CustInfoMgmt() {
        ModelAndView CustInfoMgmt = new ModelAndView("/sysUser/CustInfoMgmt");
        return CustInfoMgmt;
    }


    /*表格数据显示*/
//    搜索框功能service实现
    @GetMapping("/userList")
    public R userList(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "5") Integer limit, SysUser sysUser) {
        PageInfo<SysUser> pageInfo = service.list(sysUser, page, limit);
        return R.ok(pageInfo);
    }


//      编辑用户页面用户信息显示

    @GetMapping("/sysUser/edit")
    public ModelAndView userdetail(Integer id) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        SysUser one = service.getOne(wrapper);
        ModelAndView userdetail = new ModelAndView("/sysUser/edit", "user", one);
        return userdetail;
    }


    //    编辑用户页面用户信息修改
    @PostMapping("/sysUser/edit")
    public R edit(@RequestBody SysUser sysUser) {
        boolean b = service.updateById(sysUser);
        if (b) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    //    添加用户页面
    @GetMapping("/sysUser/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("/sysUser/add");
        return mv;
    }

    //    添加用户页面用户信息添加
    @PostMapping("/sysUser/add")
    public R add(@RequestBody SysUser sysUser) {
        int save = service.saveSysUser(sysUser);
        if (save > 0) {
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }


    //   删除功能，逻辑删除，将isdel字段改为true
    @PostMapping("/sysUser/delete")
    public R delete(@RequestBody List<Integer> ids) {
        List<SysUser> list = new ArrayList<>();
        for (Integer id : ids) {
            SysUser sysUser = new SysUser();
            sysUser.setId(id);
//            根据id查询出来的数据，将isdel字段改为true
            sysUser.setIsDel(true);
            list.add(sysUser);
        }
        boolean b = service.updateBatchById(list);
        if (b) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }
    @GetMapping("/sysUser/recover")
    public ModelAndView recover() {
        ModelAndView recover = new ModelAndView("/sysUser/recover");
        return recover;
    }
    @GetMapping("/sysUser/recoverList")
    public R recover(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "5") Integer limit, SysUser sysUser) {
        PageInfo<SysUser> pageInfo = service.selectByIsdel(sysUser, page, limit);
        return R.ok(pageInfo);
    }
    //   恢复功能，逻辑删除，将isdel字段改为false
    @PostMapping("/sysUser/recover")
    public R recover(@RequestBody List<Integer> ids) {
        List<SysUser> list = new ArrayList<>();
        for (Integer id : ids) {
            SysUser sysUser = new SysUser();
            sysUser.setId(id);
//            根据id查询出来的数据，将isdel字段改为true
            sysUser.setIsDel(false);
            list.add(sysUser);
        }
        boolean b = service.updateBatchById(list);
        if (b) {
            return R.ok("恢复成功 ");
        } else {
            return R.fail("恢复失败");
        }
    }

}


